package cn.anecansaitin.not_enough_harness.mixin;

import cn.anecansaitin.not_enough_harness.client.ModKeymappings;
import cn.anecansaitin.not_enough_harness.common.capability.harness_attribute.HarnessAttributeHandler;
import cn.anecansaitin.not_enough_harness.common.ModCapabilities;
import cn.anecansaitin.not_enough_harness.common.capability.harness_capability.HarnessCapabilityHandler;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.animal.HappyGhast;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(HappyGhast.class)
public abstract class HappyGhastMixin extends LivingEntity {
    protected HappyGhastMixin(EntityType<? extends LivingEntity> p_20966_, Level p_20967_) {
        super(p_20966_, p_20967_);
    }

    @Overwrite
    protected Vec3 getRiddenInput(Player player, Vec3 travelVector) {
//        if (player.level().isClientSide()) {
        ItemStack harness = getItemBySlot(EquipmentSlot.BODY);
        HarnessAttributeHandler cap = harness.getCapability(ModCapabilities.HARNESS_ATTRIBUTE_HANDLER);
        float x = player.xxa;
        float z = player.zza;

        if (cap == null) {
            float y = (player.isJumping() ? 0.5f : 0) + (ModKeymappings.HAPPY_GHAST_MOVE_DOWN.get().isDown() ? -0.5f : 0);
            return new Vec3(x, y, z).scale(3.9F * getAttributeValue(Attributes.FLYING_SPEED));
        }

        HappyGhast ghast = (HappyGhast) (Object) this;
        float y = (player.isJumping() ? 1f : 0) + (ModKeymappings.HAPPY_GHAST_MOVE_DOWN.get().isDown() ? -1f : 0);

        x *= x > 0 ? cap.leftSpeed(harness, ghast, player) : cap.rightSpeed(harness, ghast, player);
        y *= y > 0 ? cap.upSpeed(harness, ghast, player) : cap.downSpeed(harness, ghast, player);
        z *= z > 0 ? cap.forwardSpeed(harness, ghast, player) : cap.backwardSpeed(harness, ghast, player);
        return new Vec3(x, y, z);
//        } else {
//            return Vec3.ZERO;
//        }
    }

    @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;isClientSide()Z"))
    public void notEnoughHarness$tick(CallbackInfo ci) {
        LivingEntity living = getControllingPassenger();

        if (living == null) {
            return;
        }

        ItemStack harness = getItemBySlot(EquipmentSlot.BODY);

        if (harness.isEmpty()) {
            return;
        }

        HarnessCapabilityHandler cap = harness.getCapability(ModCapabilities.HARNESS_CAPABILITY_HANDLER);

        if (cap == null) {
            return;
        }

        cap.tick(harness, (HappyGhast) (Object) this, living);
    }
}
